package com.gohi.main.system.service.impl;
import java.util.ArrayList;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;
import org.hibernate.TransactionException;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.jdbc.support.rowset.SqlRowSet;
import com.gohi.core.service.impl.DefaultService;
import com.gohi.main.system.hbm.ShopgooArea;
import com.gohi.main.system.hbm.ShopgooAreaBusiness;
import com.gohi.main.system.hbm.ShopgooWxBrand;
import com.gohi.main.system.hbm.jdbcMapper.AreMapper;
import com.gohi.main.system.service.ISystemService;
public class SystemServiceImpl extends DefaultService<Object, String> implements ISystemService {
	@SuppressWarnings("unchecked")
	@Override
	public List<ShopgooArea> getAre() throws org.hibernate.TransactionException {
		// TODO Auto-generated method stub
		String sql ="select * from shopgoo_area where hot_city=1 and parent_area_id=0 order by area_sort desc";
		JdbcTemplate jdbc= this.getDAO().getJdbcTemplate();
		List<ShopgooArea> list= jdbc.query(sql,new AreMapper());
		
		/*SqlRowSet row= jdbc.queryForRowSet(sql);
		while(row.next()){
			System.err.println(row.getString("area_name"));
		}*/
		
		return list;
	}

	@Override
	public List<ShopgooWxBrand> getAllBrand() throws TransactionException {
		String sql ="SELECT * from shopgoo_wx_brand ORDER BY brand_sort DESC";
		
		JdbcTemplate jdbc= this.getDAO().getJdbcTemplate();
		List<ShopgooWxBrand> list= new ArrayList<ShopgooWxBrand>();
		SqlRowSet row= jdbc.queryForRowSet(sql);
		while(row.next()){
			ShopgooWxBrand brand =new ShopgooWxBrand();
			brand.setBrand_id(row.getInt("brand_id"));
			brand.setBrand_name(row.getString("brand_name"));
			brand.setBrand_pic(row.getString("brand_pic"));
			brand.setBrand_url(row.getString("brand_url"));
			brand.setBrand_sort(row.getInt("brand_sort"));
			list.add(brand);
			//System.err.println(row.getString("area_name"));
		}
		
		return list;
	}

	@Override
	public List<ShopgooAreaBusiness> getBusinessByareId(String areid) {
		String sql ="SELECT * from shopgoo_area_business where status=1 and area_id="+areid+" ORDER BY a_sort DESC";
		
		JdbcTemplate jdbc= this.getDAO().getJdbcTemplate();
		List<ShopgooAreaBusiness> list= new ArrayList<ShopgooAreaBusiness>();
		SqlRowSet row= jdbc.queryForRowSet(sql);
		while(row.next()){
			ShopgooAreaBusiness bus =new ShopgooAreaBusiness();
			bus.setName(row.getString("name"));
			bus.setId(row.getInt("id"));
			list.add(bus);
		}
		
		return list;
	}

	@Override
	public Map<String, List<ShopgooArea>> getAreByparid(String parid) {
		//System.err.println(parid);
		// TODO Auto-generated method stub
		Map<String, List<ShopgooArea>> map  =new LinkedHashMap<String, List<ShopgooArea>>();
		
		String sql ="select * from shopgoo_area where parent_area_id="+parid+" or  parent_area_id in(select area_id from shopgoo_area  where  parent_area_id="+parid+")";
		//System.err.println(sql);
		SqlRowSet rs= this.getDAO().getJdbcTemplate().queryForRowSet(sql);
		String temparename= "";
		List<ShopgooArea> list  =new ArrayList<ShopgooArea>();
		while(rs.next()){
			ShopgooArea are =new ShopgooArea();
			are.setArea_id(rs.getInt("area_id"));
			are.setParent_area_id(rs.getInt("parent_area_id"));
			are.setArea_name(rs.getString("area_name"));
			//System.err.println(are.getParent_area_id());
			if(are.getParent_area_id().toString().equals(parid)){
				//如果是父类
				temparename= are.getArea_name();
				if(map.containsKey(temparename)==false){
					//如果是第一次
					list  =new ArrayList<ShopgooArea>();
					are.setParent_area_id(0);
					are.setArea_name(temparename+"-全部");
					list.add(are);
					map.put(temparename, list);
				}
			}else{
				list.add(are);
				map.put(temparename, list);
			}
		}
		return map;
	}
	
	

}
